package cn.xerllent.wxtp.handler;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;

import cn.xerllent.wxtp.entity.WxReport;
import cn.xerllent.wxtp.service.DksbReportService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.bean.WxCpTpIdTranslateResultResp;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

/**
 * 批量任务事件处理器.
 *
 */
@Component
public class BatchJobHandler extends AbstractHandler {
	@Resource
	private DksbReportService dksbReportService;

  @Override
  public WxCpXmlOutMessage handle(WxCpTpXmlMessage wxMessage, Map<String, Object> context, WxCpTpService wxCpTpService,
                                  WxSessionManager sessionManager) {
		String suiteId = wxCpTpService.getWxCpTpConfigStorage().getSuiteId();
		String corpId = wxMessage.getAuthCorpId(); // 授权企业id
		String jobType = wxMessage.getBatchJob().getJobType();
		String jobId = wxMessage.getBatchJob().getJobId();

		if(jobType.equals("contact_id_translate") ) {
			//通讯录转译,适用于所有业务的报表
			try {
				WxCpTpIdTranslateResultResp  result = wxCpTpService.getWxCpTpContactService().getBatchResult(jobId);
				WxReport r = dksbReportService.getOne(new LambdaUpdateWrapper<WxReport>().eq(WxReport::getJobid , jobId));

				if(r.getStatus()<4) {  //没有回调返回时
					r.setUrl(result.getResult().getContactIdTranslate().getUrl()).setStatus(4);
					dksbReportService.saveOrUpdate(r);
				}
			} catch (WxErrorException e) {
				e.printStackTrace();
			}
		}else {
			// 区分业务
			if (suiteId.equals("wwa96d2ab2354ee862")) { // 代课申报

			}
		}

    return null;
  }

}
